<?php
	global $AppUI, $project_id, $df, $m, $tab, $isProjectCompleted, $canEdit, $canCreateTasks, $hasRoleFfifaMana;

	$perms = & $AppUI->acl();
	$canCreateTasks = $perms->checkModuleItem('tasks', 'add');
	$canCreateLogs = $perms->checkModuleItem('task_log', 'add');
	require_once($AppUI->getModuleClass('tasks'));
	// TODO:
	// Load expenseList form SysVal
	$expenseList = dPgetSysVal('TaskLogExpense');
	// Lets check which cost codes have been used before
	$q  = new DBQuery;
	$q->addQuery('project_company');
	$q->addTable('projects');
	$q->addWhere('project_id = ' . $project_id);
	$company_id = $q->loadResult();

	$q->addTable('billingcode');
	$q->addQuery('billingcode_id, billingcode_name');
	$q->addOrder('billingcode_name');
	$q->addWhere('billingcode_status = 0');
	$q->addWhere('(company_id = 0 OR company_id = ' . $company_id . ')');
	$task_log_costcodes = $q->loadHashList();

 	// Show deleted codes separately (at the end)
	$q->addTable('billingcode');
	$q->addQuery('billingcode_id, billingcode_name');
	$q->addOrder('billingcode_name');
	$q->addWhere('billingcode_status = 1');
	$q->addWhere('(company_id = 0 OR company_id = ' . $company_id . ')');
	$task_log_costcodes = arrayMerge(array('' => 'None'), $task_log_costcodes, $q->loadHashList());

	$q  = new DBQuery;
	$q->addTable('users', 'u');
	$q->addQuery('user_id, concat(contact_first_name," ",contact_last_name)');
	$q->addJoin('contacts', 'c', 'u.user_contact = c.contact_id');
	$q->addJoin('project_contacts', 'p', 'p.contact_id = u.user_id');
	$q->addOrder('contact_first_name, contact_last_name');
	$q->addWhere('p.project_id = '.$project_id);

	if (count($_POST) > 0) {
		$AppUI->savePOST('REFERENCE_TASK_LOG_POST_'.$project_id);
	}
	else if (count($_GET) == 0){
		$_POST = $AppUI->getPOST('REFERENCE_TASK_LOG_POST_'.$project_id);
	}
	$expense = dPgetParam( $_POST, 'expense');

?>
<script language="JavaScript">
<? if ($canCreateLogs) { ?>
function doDeleteTaskLog(id)
{
	if (confirm( "<?=$AppUI->_('doDelete', UI_OUTPUT_JS).' '.$AppUI->_('Task Log', UI_OUTPUT_JS).'?'?>" )) {
		document.frmLogDelete.task_log_id.value = id;
		document.frmLogDelete.del.value = 1;
		document.frmLogDelete.submit();
	}
}
<? } ?>

function doShowTaskLog(task, log, tab){
	go('?m=timecard&tab=1&tid='+log);
}
</script>

<table border="0" cellpadding="2" cellspacing="0" width="100%" class="infopanel">
<form name="frmFilter" action="index.php?m=projects&amp;a=view&project_id=<?=$project_id?>&tab=<?=$tab?>" method="POST">
<tr>
	<td width="98%">
	<?php
	if ($m == 'projects' && $canCreateLogs && !$isProjectCompleted){
		echo "<input type='button' value='New Task Log' onclick='goNewTaskLog($project_id)' class='button'/>";
	} else {
		echo "&nbsp;";
	}?>
	</td>
	<td width="1%">
		<input id="idHC" type="checkbox" name="expense[]" <?=is_array($expense) && in_array(1, $expense) ?'checked':''?>
			value="1" onchange="document.frmFilter.submit()">
	</td>
	<td nowrap="nowrap"><label for="idHC"><?=$AppUI->_('Inter expense')?></label></td>
	<td width="1%">
		<input id="idHC" type="checkbox" name="expense[]" <?=is_array($expense) && in_array(2, $expense)?'checked':''?>
			value="2" onchange="document.frmFilter.submit()">
	</td>
	<td nowrap="nowrap"><label for="idHC"><?=$AppUI->_('On behalf expense')?></label></td>
	<td width="1%">
		<input id="idHC" type="checkbox" name="expense[]" <?=is_array($expense) && in_array(3, $expense)?'checked':''?>
			value="3" onchange="document.frmFilter.submit()">
	</td>
	<td nowrap="nowrap"><label for="idHC"><?=$AppUI->_('Other expense')?></label></td>
	<td><img src="images/blank.gif" width="8"></td>
</tr>
<!--
<input type="hidden" name="m" value="projects"/>
<input type="hidden" name="a" value="view"/>
<input type="hidden" name="project_id" value="<?=$project_id?>"/>
<input type="hidden" name="tab" value="<?=$tab?>"/>
-->
</form>
</table>
<?php
// Winnow out the tasks we are not allowed to view.
$perms =& $AppUI->acl();
$project =& new CProject;

// Pull the task comments
$q  = new DBQuery;
$q->addTable('task_log');
$q->addQuery('task_log.*, user_username, ite.item_name, divisions.div_name, cc.cc_name');
$q->addJoin('items', 'ite', 'ite.item_id = task_log_item');
$q->addJoin('divisions', 'divisions', 'divisions.div_id = task_log_division');
$q->addJoin('users', 'u', 'user_id = task_log_creator');
$q->addJoin('costcenters', 'cc', 'cc.cc_id = task_log_costcenter');
$q->addWhere('task_log_project = '.$project_id);
if (is_array($expense) && count($expense) > 0) {
	$q->addWhere('task_log_expense in (' . implode(',', $_POST['expense']) . ')');
}
$needCheckCostCenter = $perms->userHasRole($AppUI->user_id, "FFIA_Staff") || $perms->userHasRole($AppUI->user_id, "Operation");
if ($needCheckCostCenter) {
	$q->addWhere("task_log_costcenter = {$AppUI->user_costcenter}");
}
$q->addOrder('task_log_date');
$logs = $q->loadList();
?>
<table id="idTaskLogsList" border="0" cellpadding="3" cellspacing="0" width="100%" class="list">
<form name="frmLogDelete" action="index.php?m=timecard" method="post">
	<input type="hidden" name="m" value="timecard">
	<input type="hidden" name="dosql" value="do_updatetasklog">
	<input type="hidden" name="del" value="0" />
	<input type="hidden" name="task_log_id" value="0" />
</form>
<tr>
	<th></th>
	<!--th><?=$AppUI->_('Date')?></th-->
	<th align="right">%</th>
	<th nowrap="nowrap"><?=$AppUI->_('Item Name')?></th>
	<th width="100px" nowrap="nowrap"><?=$AppUI->_('Expense type')?></th>
	<th width="100px"><?=$AppUI->_('Expense Amount')?></th>
	<th width="100px"><?=$AppUI->_('Division')?></th>
	<th nowrap="nowrap"><?=$AppUI->_('Cost Center')?></th>
	<th width="200px"><?=$AppUI->_('Resource')?></th>
	<th><?=$AppUI->_('Description')?></th>
	<!--th width="100%"><?=$AppUI->_('Remarks')?></th-->
	<th></th>
</tr>
<?php
$s = '';
$hrs = 0;
$amount_total = 0;
if (count($logs) == 0) {
	showEmptyRow(9, 'second');
}
else
foreach ($logs as $row) {
	$mylog = $row['task_log_creator'] == $AppUI->user_id;
	// trangdt
	$my_cc_log = $needCheckCostCenter ? $row['task_log_costcenter'] == $AppUI->user_costcenter : TRUE;
	// end trangdt
	$task_log_date = intval( $row['task_log_date'] ) ? new CDate( $row['task_log_date'] ) : null;
?>
<tr class="second">
<td align="center">
<?php
	//if ($perms->checkModuleItem('tasks', 'edit', $row['task_id']) && $mylog && $row['task_is_complete']<1) {
	if ($perms->checkModuleItem('task_log', 'edit') && $my_cc_log && $row['task_is_complete']<1) {
	showIconLink('images/edit_log.gif', 'js:doShowTaskLog('.$row['task_id'].','.$row['task_log_id'].',1)', 'Edit');
	}
	else {
		showIconLink('images/view.gif', 'js:doShowTaskLog('.$row['task_id'].','.$row['task_log_id'].',0)', 'View');
	}
?>
</td>
<!--td nowrap="nowrap"><?=$task_log_date ? $task_log_date->format( $df ) : '-'?></td-->
<td align="right"><?=$row['task_percent_complete']?>%</td>
<td><a href="?m=tasks&a=view&task_id=<?=$row['task_id']?>&tab=0"><?=$row['item_name']?></a></td>
<td><?=$expenseList[$row["task_log_expense"]]?></td>
<td align="right"><?=number_format($row['task_log_amount'])?></td>
<td align="right"><?=$row['div_name']?></td>
<td align="right"><?=$row['cc_name']?></td>
<td><?=$row['user_username']?></td>
<td><?=str_replace( "\n", '<br/>', $row['task_log_description'] )?></td>
<td align="center">
<?php
	if ($perms->checkModuleItem('task_log', 'delete') && $my_cc_log && $row['task_is_complete']<1) {
		showIconLink('images/delete_log.gif', 'js:doDeleteTaskLog('.$row['task_log_id'].')', 'Delete');
	}
	else {
		echo '<img src="images/nodel_log.gif" title="No delete">';
	}
?>
</td>
</tr>
<?php
	//$hrs += (float)$row['task_log_hours'];
	$amount_total += (float)$row['task_log_amount'];
}
?>
<tr bgcolor="white" valign="top">
<td colspan="4" align="right"><?=$AppUI->_('Total Amounts')?></td>
<td align="right"><?=number_format($amount_total )?></td>
<td colspan="100%">&nbsp;</td>
</tr>
</table>
<script>
	Calendar.enabled = true;

	Table("idTaskLogsList").setNormalHover(1);

	function showTask(id)
	{
		location.href = "index.php?m=tasks&a=view&task_id=" + id;
	}

	function doSearch(frm) {
		frm.action = '?m=projects&tab=1&suppressHeaders=0';
		frm.submit();
	}

	function doExport(frm) {
		//frm.action = '?m=reports&a=do_r_reference&suppressHeaders=1';
		//frm.submit();
	}
</script>
